package com.wq.myspring.loopdepend;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * @PackageName: com.wq.myspring.loopdepend
 * @ClassName Mytest
 * @Description: TODO spring不支持显示定义的循环依赖@DependsOn, 不支持prototype模式下的循环依赖, 不支持构造器产生的循环依赖
 * @Author Wenqiang
 * @Date 2021/07/18 12:06
 * @Version V1.0
 */
@ComponentScan("com.wq.myspring.loopdepend")
public class Mytest {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Mytest.class);
		A a = context.getBean("a", A.class);
		System.out.println("a ==> " + a);
		System.out.println("a ==> " + a.getB());
	}
}

@Component
// @DependsOn("b")  //spring不支持显示定义的循环依赖, 源码中会直接抛出异常
class A {
	@Autowired
	private B b;

	public B getB() {
		return b;
	}

	public void setB(B b) {
		this.b = b;
	}
}

@Component
// @DependsOn("a") //spring不支持显示定义的循环依赖, 源码中会直接抛出异常
class B {
	@Autowired
	private A a;

	public A getA() {
		return a;
	}

	public void setA(A a) {
		this.a = a;
	}
}